package com.ddzh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddzh.dto.RoleDto;
import com.ddzh.dto.UserRoleDto;
import com.ddzh.mapper.UserRoleMapper;
import com.ddzh.pojo.Role;
import com.ddzh.pojo.UserRole;
import com.ddzh.service.UserRoleService;
import org.apache.dubbo.config.annotation.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {
    @Override
    public boolean deleteByUserId(Long userId) {
        UserRole userRole = new UserRole();
        userRole.setId(userId);
        userRole.setIs_delete(1);
        return this.updateById(userRole);
    }

    @Override
    public Long[] findRoleIdsByUserId(Long userId) {
        return this.baseMapper.findRoleIdsByUserId(userId);
    }

    @Override
    public boolean deleteByRoleId(Long roleId) {
        UserRole userRole = new UserRole();
        userRole.setId(roleId);
        userRole.setIs_delete(1);
        return this.updateById(userRole);
    }

    @Override
    public boolean allocateRole(UserRoleDto userRoleDto) {
        //清除原来维护的 用户-角色 关系
//        deleteByUserId(userRoleDto.getUserId());
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userRoleDto.getUserId());
        boolean b = this.remove(queryWrapper);
        List<UserRole> list = this.list(queryWrapper);
        for (UserRole userRole : list) {
            System.out.println("userRole.toString() = " + userRole.toString());
        }
        //增加新的 用户-角色 关系,批量向中间表中插入数据
        ArrayList<UserRole> userRoles = new ArrayList<>();
        if (userRoleDto.getRoleIds().length>0) {
            UserRole userRole = new UserRole();
            for (Long roleId : userRoleDto.getRoleIds()) {
                userRole.setUserId(userRoleDto.getUserId());
                userRole.setRoleId(roleId);
                userRoles.add(userRole);
            }
        }
        return this.saveOrUpdateBatch(userRoles);
    }

    @Override
    public Long[] findUserIdsByRoleId(Long roleId) {

        return this.baseMapper.findUserIdsByRoleId(roleId);
    }

    @Override
    public boolean allocateUser(UserRoleDto userRoleDto) {

        //移除 角色旧的用户关系
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",userRoleDto.getRoleId());
        this.remove(queryWrapper);

        //新增角色-用户关系
        List<UserRole> userRoleList = new ArrayList<>();
        if (userRoleDto.getUserIds().length>0){
            for (Long userId : userRoleDto.getUserIds()) {
                UserRole userRole = new UserRole();
                userRole.setUserId(userId);
                userRole.setRoleId(userRoleDto.getRoleId());
                userRoleList.add(userRole);
            }
            this.saveBatch(userRoleList);
        }
        return true;
    }
}
